package ar.edu.unq.tpi.uis.tepe3.wicket.pages;

import java.util.List;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;

import ar.edu.unq.tpi.uis.tepe3.dominio.SvnManager;
import ar.edu.unq.tpi.uis.tepe3.dominio.User;
import ar.edu.unq.tpi.uis.tepe3.wicket.AbstractListPage;
import ar.edu.unq.tpi.uis.tepe3.wicket.WicketApplication;
import ar.edu.unq.tpi.uis.tepe3.wicket.panels.DefaultCreateNewMenuPanel;
import ar.edu.unq.tpi.uis.tepe3.wicket.panels.DefaultListViewActionsPanel;

public class UserListPage extends AbstractListPage {

	public UserListPage() {
		
		super("Listado de usuarios");
		
		add(new ListView("listview", getListViewDataSource()) {
            protected void populateItem(ListItem item) {
                User aUser= (User) item.getModelObject();                
                item.add(new Label("name", aUser.getName()));
                item.add(new Label("id", String.valueOf(aUser.getId())));
                item.add(new DefaultListViewActionsPanel("actions", item.getModel()));
            }
        });
		
    }
	
	/**
	 * @return
	 */
	private List<User> getListViewDataSource() {		
		SvnManager aSvnManager = ((WicketApplication)this.getApplication()).getModel();
		return aSvnManager.getUsers();
	}

	@Override
	public void deleteAction(IModel model) {
		try {
			((WicketApplication) UserListPage.this.getApplication()).getModel().removeUser((User) model.getObject());
			((WicketApplication) UserListPage.this.getApplication()).save();
			deleteSucceed();
		} catch (Exception e) {
			warn(e.getMessage());
		}
	}

	@Override
	protected void addLeftMenu() {
		DefaultCreateNewMenuPanel aMenu = new DefaultCreateNewMenuPanel("leftMenu", new CompoundPropertyModel(new User("","","")));
		add(aMenu);
	}

}